home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / asmutil / disasm.zip / ASMTUTR2.DOC < prev    next >
Text File  |  1988-06-03  |  25KB  |  429 lines

  1.  
  2.         so.  An exception is the saving and restoring of registers at
  3.         entrance to and exit from a subroutine; here, if the subroutine is
  4.         long, you should probably PUSH everything which the caller may need
  5.         saved, whether you will use the register or not, and POP it in
  6.         reverse order at the end.
  7.         Be aware that CALL and INT push return address information on the
  8.         stack and RET and IRET pop it off.  It is a good idea to become
  9.         familiar with the structure of the stack.
  10.     c.  In practice, to invoke system services you will use the INT
  11.         instruction.  It is quite possible to use this instruction effec-
  12.         tively in a cookbook fashion without knowing precisely how it
  13.         works.
  14.     d.  The transfer of control instructions (CALL, RET, JMP) deserve care-
  15.         ful study to avoid confusion.  You will learn that these can be
  16.         classified as follows:
  17.         1)  all three have the capability of being either NEAR (CS register
  18.             unchanged) or FAR (CS register changed)
  19.         2)  JMPs and CALLs can be DIRECT (target is assembled into instruc-
  20.             tion) or INDIRECT (target fetched from memory or register)
  21.         3)  if NEAR and DIRECT, a JMP can be SHORT (less than 128 bytes
  22.             away) or LONG
  23.         In general, the third issue is not worth worrying about.  On a for-
  24.         ward jump which is clearly VERY short, you can tell the assembler
  25.         it is short and save one byte of code:
  26.                    JMP SHORT  CLOSEBY
  27.         On a backward jump, the assembler can figure it out for you.  On a
  28.         forward jump of dubious length, let the assembler default to a LONG
  29.         form; at worst you waste one byte.
  30.         Also leave the assembler to worry about how the target address is
  31.         to be represented, in absolute form or relative form.
  32.     e.  The conditional jump set is rather confusing when studied apart
  33.         from the assembler, but you do need to get a feeling for it.  The
  34.         interactions of the sign, carry, and overflow flags can get your
  35.         mind stuttering pretty fast if you worry about it too much.  What
  36.         is boils down to, though, is
  37.                 JZ        means what it says
  38.                 JNZ       means what it says
  39.                 JG reater this means "if the SIGNED difference is positive"
  40.                 JA bove   this means "if the UNSIGNED difference is positive"
  41.                 JL ess    this means "if the SIGNED difference is negative"
  42.                 JB elow   this means "if the UNSIGNED difference is negative"
  43.                 JC arry   assembles the same as JB; it's an aesthetic choice
  44.  
  45. IBM PC Assembly Language Tutorial                                        10
  46.  
  47.  
  48.         You should understand that all conditional jumps are inherently
  49.         DIRECT, NEAR, and "short"; the "short" part means that they can't
  50.         go more than 128 bytes in either direction.  Again, this is some-
  51.         thing you could easily imagine to be more of a problem than it is.
  52.         I follow this simple approach:
  53.         1)  When taking an abnormal exit from a block of code, I always use
  54.             an unconditional jump.  Who knows how far you are going to end
  55.             up jumping by the time the program is finished.  For example, I
  56.             wouldn't code this:
  57.                    TEST     AL,IDIBIT       ;Is the idiot bit on?
  58.                    JNZ      OYVEY           ;Yes.  Go to general cleanup
  59.             Rather, I would probably code this:
  60.                    TEST     AL,IDIBIT       ;Is the idiot bit on?
  61.                    JZ       NOIDIOCY        ;No.  I am saved.
  62.                    JMP      OYVEY           ;Yes.  What can we say...
  63.               NOIDIOCY:
  64.             The latter, of course, is a jump around a jump.  Some would say
  65.             it is evil, but I submit it is hard to avoid in this language.
  66.         2)  Otherwise, within a block of code, I use conditional jumps
  67.             freely.  If the block eventually grows so long that the assem-
  68.             bler starts complaining that my conditional jumps are too long
  69.             I
  70.             a)  consider reorganizing the block but
  71.             b)  also consider changing some conditional jumps to their
  72.                 opposite and use the "jump around a jump" approach as shown
  73.                 above.
  74.     Enough about specific instructions!
  75. 6.  Finally, in order to use the assembler effectively, you need to know
  76.     the default rules for which segment registers are used to complete
  77.     addresses in which situations.
  78.     a.  CS is used to complete an address which is the target of a NEAR
  79.         DIRECT jump.  On an NEAR INDIRECT jump, DS is used to fetch the
  80.         address from memory but then CS is used to complete the address
  81.         thus fetched.  On FAR jumps, of course, CS is itself altered.  The
  82.         instruction counter is always implicitly pointing in the code seg-
  83.         ment.
  84.     b.  SS is used to complete an address if BP is used in its formation.
  85.         Otherwise, DS is always used to complete a data address.
  86.     c.  On the string instructions, the target is always formed from ES and
  87.         DI.  The source is normally formed from DS and SI.  If there is a
  88.         segment prefix, it overrides the source not the target.
  89.  
  90. IBM PC Assembly Language Tutorial                                        11
  91.  
  92.  
  93. Learning about DOS
  94. __________________
  95. Learning about DOS
  96. Learning about DOS
  97. Learning about DOS
  98. I think the best way to learn about DOS internals is to read the technical
  99. appendices in the manual.  These are not as complete as we might wish, but
  100. they really aren't bad; I certainly have learned a lot from them.  What you
  101. don't learn from them you might eventually learn via judicious disassembly
  102. of parts of DOS, but that shouldn't really be necessary.
  103. From reading the technical appendices, you learn that interrupts 20H
  104. through 27H are used to communicate with DOS.  Mostly, you will use inter-
  105. rupt 21H, the DOS function manager.
  106. The function manager implements a great many services.  You request the
  107. individual services by means of a function code in the AH register.  For
  108. example, by putting a nine in the AH register and issuing interrupt 21H you
  109. tell DOS to print a message on the console screen.
  110. Usually, but by no means always, the DX register is used to pass data for
  111. the service being requested.  For example, on the print message service
  112. just mentioned, you would put the 16 bit address of the message in the DX
  113. register.  The DS register is also implicitly part of this argument, in
  114. keeping with the universal segmentation rules.
  115. In understanding DOS functions, it is useful to understand some history and
  116. also some of the philosophy of MS-DOS with regard to portability.  General-
  117. ly, you will find, once you read the technical information on DOS and also
  118. the IBM technical reference, you will know more than one way to do almost
  119. anything.  Which is best?  For example, to do asynch adapter I/O, you can
  120. use the DOS calls (pretty incomplete), you can use BIOS, or you can go
  121. directly to the hardware.  The same thing is true for most of the other
  122. primitive I/O (keyboard or screen) although DOS is more likely to give you
  123. added value in these areas.  When it comes to file I/O, DOS itself offers
  124. more than one interface.  For example, there are four calls which read data
  125. from a file.
  126. The way to decide rationally among these alternatives is by understanding
  127. the tradeoffs of functionality versus portability.  Three kinds of porta-
  128. bility need to be considered:  machine portability, operating system porta-
  129. bility (for example, the ability to assemble and run code under CP/M 86)
  130. and DOS version portability (the ability for a program to run under older
  131. versions of DOS>.
  132. Most of the functions originally offered in DOS 1.0 were direct descendents
  133. of CP/M functions; there is even a compatibility interface so that programs
  134. which have been translated instruction for instruction from 8080 assembler
  135. to 8086 assembler might have a reasonable chanc